package com.example.dllo.exe.fenlei.xuanlishenqi;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.PopupWindowBean;
import com.example.dllo.exe.bean.PopupWindowPinJieBean;
import com.example.dllo.exe.bean.XuanLiShenQiBean;
import com.example.dllo.exe.fengzhuang.NetTool;
import com.example.dllo.exe.main.BaseAty;
import com.example.dllo.exe.fengzhuang.DBValue;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class XuanLiShenQiAty extends BaseAty implements View.OnClickListener, OnItemClickPW {
    private RecyclerView recyclerViewxunlishenqi;
    private ImageView rvImageViewxuanlishenqi;
    public Button duixiang, changhe, gexing, jiage;
    private PopupWindow pw;
    private PopupWindowBean popupWindowBean;
    private GridView gridViewpop;
    private PopupwindowAdapter adapter;
     public int type;
    private NetTool netTool;



    @Override
    protected int setLayout() {
        return R.layout.fenleixuanlishenqilayout;
    }

    @Override
    protected void initView() {
        recyclerViewxunlishenqi = bindView(R.id.xuanliShenqi_Rv);
        rvImageViewxuanlishenqi = bindView(R.id.tiaoxuanliwuback);
        duixiang = bindView(R.id.dui_xiang);
        changhe = bindView(R.id.chang_he);
        gexing = bindView(R.id.ge_xing);
        jiage = bindView(R.id.jia_ge);

    }

    @Override
    protected void initData() {
        NetTool netTool = new NetTool();
        netTool.getNetData(DBValue.FENLEI_XUANLISHENQI, XuanLiShenQiBean.class, new NetTool.NetListener<XuanLiShenQiBean>() {
            @Override
            public void onSuccess(XuanLiShenQiBean xuanLiShenQiBean) {
                XuLiShenQiRVAdapter xuLiShenQiRVAdapter = new XuLiShenQiRVAdapter(getApplicationContext());
                GridLayoutManager manager = new GridLayoutManager(getApplicationContext(), 2);
                xuLiShenQiRVAdapter.setXuanLiShenQiBean(xuanLiShenQiBean);
                recyclerViewxunlishenqi.setAdapter(xuLiShenQiRVAdapter);
                recyclerViewxunlishenqi.setLayoutManager(manager);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        rvImageViewxuanlishenqi.setOnClickListener(this);
        duixiang.setOnClickListener(this);
        changhe.setOnClickListener(this);
        gexing.setOnClickListener(this);
        jiage.setOnClickListener(this);

        initmPopupWindowView();

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.tiaoxuanliwuback:

                finish();

                return;
            case R.id.jia_ge://3
                type = 3;

                break;
            case R.id.ge_xing://2
                type = 2;
                break;
            case R.id.chang_he://1
                type = 1;
                break;
            case R.id.dui_xiang://0
                type = 0;
                break;


        }
        adapter.setType(type);

        if (pw.isShowing()) {
            pw.dismiss();

        } else {
//            Toast.makeText(this, "show", Toast.LENGTH_SHORT).show();
            pw.showAsDropDown(view, 0, 0);

        }
    }

    private void initmPopupWindowView() {
        netTool = new NetTool();
        popupWindowBean = new PopupWindowBean();
        netTool.getNetData(DBValue.FENLEI_XUANLISHENQIPW, PopupWindowBean.class, new NetTool.NetListener<PopupWindowBean>() {
            @Override
            public void onSuccess(final PopupWindowBean popupWindowBean) {
                View popView = LayoutInflater.from(XuanLiShenQiAty.this).inflate(R.layout.pop_upwindow, null);
                adapter = new PopupwindowAdapter(XuanLiShenQiAty.this);
                adapter.setOnItemClickPW(XuanLiShenQiAty.this);
                gridViewpop = (GridView) popView.findViewById(R.id.grid_viewtiaoxuanliwu);



                pw = new PopupWindow(popView, 800, 1000);
                adapter.setPopupWindowBean(popupWindowBean);
                adapter.setXuanLiShenQiActivity(XuanLiShenQiAty.this);

                //点击其他位置消失

                pw.setOutsideTouchable(true);
                gridViewpop.setAdapter(adapter);

                popView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        if (pw != null && pw.isShowing()) {
                            pw.dismiss();

                        }

                        return false;
                    }
                });

            }

            @Override
            public void onError(String errorMsg) {

            }
        });


    }

    String DX = "";
    String CH = "";
    String GX = "";
    String JG = "";

    @Override
    public void OnTtemClickPW(String title, String key) {

        switch (type) {
            case 0:
                DX = "&" + title + "=" + key;
                break;

            case 1:
                CH = "&" + title + "=" + key;
                break;

            case 2:
                GX = "&" + title + "=" + key;
                break;

            case 3:
                JG = "&" + title + "=" + key;
                break;
        }
        netTool = new NetTool();
        netTool.getNetData(DBValue.FENLEI_XUANLISHENQI + DX + CH + GX + JG, PopupWindowPinJieBean.class, new NetTool.NetListener<PopupWindowPinJieBean>() {
            @Override
            public void onSuccess(PopupWindowPinJieBean popupWindowPinJieBean) {
                String url = DBValue.FENLEI_XUANLISHENQI + DX + CH + GX + JG;
                Log.d("XuanLiShenQiAty", url);
                PopupWindowPinJieAdapter popupWindowPinJieAdapter = new
                        PopupWindowPinJieAdapter(getApplicationContext());
                popupWindowPinJieAdapter.setPopupWindowPinJieBean(popupWindowPinJieBean);
                GridLayoutManager manager = new GridLayoutManager(getApplicationContext(), 2);
                recyclerViewxunlishenqi.setLayoutManager(manager);
                recyclerViewxunlishenqi.setAdapter(popupWindowPinJieAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        pw.dismiss();
    }
}
